package com.wangsd.web.sys.service;

import com.gitee.fastmybatis.core.query.Query;
import com.gitee.fastmybatis.core.util.MyBeanUtil;
import com.wangsd.web.sys.entity.AgentFlow;
import com.wangsd.web.sys.mapper.AgentFlowMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AgentFlowService {
    @Resource
    AgentFlowMapper mapper;

    public int save(Long customerId, Integer settleAmount) {
        Date date = new Date();
        AgentFlow agentFlow = new AgentFlow();
        agentFlow.setSettleAmount(settleAmount);
        agentFlow.setAgentId(customerId);
        agentFlow.setStatus(2);
        agentFlow.setCreatedTime(date);
        agentFlow.setCompletedTime(date);
        return mapper.saveIgnoreNull(agentFlow);
    }

    public List<AgentFlow> selectPage(AgentFlow queryObj, String code) {
        Query query = new Query();
        if (queryObj.getAgentId() != null) {
            query.eq("t.agent_id", queryObj.getAgentId());
        }
        if (StringUtils.isNotBlank(code)) {
            query.likeRight("t2.code", code);
        }
        query.join("join sys_company t2 on t.agent_id = t2.id");
        List<String> column = Arrays.asList("t.*, t2.name as agentName");
        List<Map<String, Object>> mapList = mapper.listMap(column, query);
        List<AgentFlow> list = MyBeanUtil.mapListToObjList(mapList, AgentFlow.class);
        return list;
    }
}
